package com.singzi.leetcode.N900;

import java.util.ArrayList;
import java.util.Arrays;

public class T0825 {
    public static void main(String[] args) {
        int[] nums = {73,106,39,6,26,15,30,100,71,35,46,112,6,60,110};
        Solution0825 solution0825 = new Solution0825();
        System.out.println(solution0825.numFriendRequests(nums));
    }


}
class Solution0825 {
    public int numFriendRequests(int[] ages) {
        Arrays.sort(ages);

        System.out.println(Arrays.toString(ages));
        int count = 0;
        double boundary;
        int left, right, mid;
        int length = ages.length;
        for (int x = 0; x < length; x++) {
            boundary = 0.5*ages[x] +7;
            left = 0;
            for(int y = x+1;y<length;y++){
                if(ages[y] == ages[x]&&ages[x]>=15) {
                    count++;
                }
            }
            if(x==0) continue;
            right = x-1;

            while (left <= right){
                mid = left + (right - left)/2;
                if(boundary < ages[mid]){
                   right = mid-1;
                }
                else{
                   left = mid+1;
                }
            }
            if(ages[left] <= boundary) continue;
            count += x-left;
        }
        return count;

    }
}